package cn.alusiaor.dic.service.impl;

import cn.alusiaor.dic.domain.DO.Dic;
import cn.alusiaor.dic.mapper.DictMapper;
import cn.alusiaor.dic.service.IDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DictService extends ServiceImpl<DictMapper, Dic> implements IDictService {

    public static Map<String, Dic> dicMap = new HashMap<>();

    @PostConstruct
    public void initDicMap() {
        List<Dic> list = this.list();
        dicMap = list.stream().collect(Collectors.toMap(Dic::getId, Function.identity()));
    }

    public Dic getDic(String id) {
        return dicMap.getOrDefault(id, Dic.builder().id(id).build());
    }

}
